package com.example.a2032.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.activity.ChatActivity;
import com.example.a2032.adapter.ChatItemAdapter;
import com.example.a2032.adapter.GroupItemAdapter;
import com.example.a2032.entity.ImResultData;
import com.example.a2032.livedatas.LiveDataBus;
import com.example.a2032.net.jstylenet.NetSendUtils;
import com.example.a2032.viewmodel.BindChatViewModel;
import com.hyphenate.easeui.constants.EaseConstant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ChatListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ChatListFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.RecyclerView_List)
    RecyclerView RecyclerViewList;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private BindChatViewModel mViewModel;
    @BindArray(R.array.kinship_array)
    String[]knshipArray;
    private GroupItemAdapter groupItemAdapter;

    public ChatListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ChatListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ChatListFragment newInstance(String param1, String param2) {
        ChatListFragment fragment = new ChatListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_chat_list, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    ChatItemAdapter chatItemAdapter;
    private static final String TAG = "ChatListFragment";
    private void init() {
        int[]iconArrays=Utils.getResourcesImageArray(getContext(),R.array.item_chatImgId);
        chatItemAdapter = new ChatItemAdapter(new ArrayList<>(),knshipArray,iconArrays);
        groupItemAdapter = new GroupItemAdapter(new ArrayList<>());
        ConcatAdapter concatAdapter=new ConcatAdapter();
        concatAdapter.addAdapter(groupItemAdapter);
        concatAdapter.addAdapter(chatItemAdapter);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerViewList.setLayoutManager(linearLayoutManager);
        RecyclerViewList.setAdapter(concatAdapter);
        mViewModel = new ViewModelProvider(this).get(BindChatViewModel.class);
        mViewModel.getAllContact().observe(getViewLifecycleOwner(), new Observer<List<ImResultData.BindersBean>>() {
            @Override
            public void onChanged(List<ImResultData.BindersBean> bindersBeans) {
                    chatItemAdapter.updateData(bindersBeans);
            }
        });
        mViewModel.getAllGroups().observe(getViewLifecycleOwner(), new Observer<List<ImResultData.GroupsBean>>() {
            @Override
            public void onChanged(List<ImResultData.GroupsBean> groupsBeans) {
                if (groupsBeans != null&&groupsBeans.size()!=0){
                    groupItemAdapter.updateData(groupsBeans);
                }else {
                    groupItemAdapter.clear();
                }

            }
        });
        chatItemAdapter.setOnItemClickListener(new ChatItemAdapter.onItemClickListener() {
            @Override
            public void onItemClick(ImResultData.BindersBean contact) {
                Log.i(TAG, "onItemClick: "+contact.getIm());
                ChatActivity.actionStart(getContext(), contact.getIm(),knshipArray[contact.getKinship()], EaseConstant.CHATTYPE_SINGLE);
            }
        });
        groupItemAdapter.setOnItemClickListener(new GroupItemAdapter.onItemClickListener() {
            @Override
            public void onItemClick(ImResultData.GroupsBean contact) {
                Log.i(TAG, "onItemClick:group "+contact.getIm());
                ChatActivity.actionStart(getContext(), contact.getIm(), contact.getName(),EaseConstant.CHATTYPE_GROUP);
            }
        });
        WebSocketUtils.getInstance().sendMsg(NetSendUtils.getIm());
    }
}